<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="车辆编号">
              <a-input v-model="queryParam.bikeNumber" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="车辆状态">
              <a-select v-model="queryParam.bikeState" placeholder="请选择" :options="bikeStates">
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="24">
              <a-form-item label="目前所在站点">
                <a-select v-model="queryParam.stationId" placeholder="请选择">
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons"
                  :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              <a @click="toggleAdvanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="$refs.addBike.add()">新建</a-button>
      <a-button type="dashed" @click="tableOption">{{ optionAlertShow && '关闭' || '开启' }}提示</a-button>
      <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="remove">
            <remove-bike ref="removeBike" :ids="selectedRowKeys.join(',')" @ok="handleOk"/>
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作
          <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>

    <s-table
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        showPagination="auto"
    >
      <span slot="bikeState" slot-scope="text">
        <a-tag :color="text | stateColorFilter">{{ text | stateTextFilter }}</a-tag>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical"/>
          <a @click="handleUpdateState(record)" :disabled="record.bikeState === 3">处理</a>
          <a-divider type="vertical"/>
          <a @click="gotoBikeList(record)">记录</a>
        </template>
      </span>
    </s-table>
    <add-bike ref="addBike" @ok="handleOk"/>
    <edit-bike ref="editBike" @ok="handleOk" :options="bikeStates"/>
    <update-state-bike ref="updateStateBike" @ok="handleOk" :options="bikeStates"/>
  </a-card>
</template>

<script>
import {STable} from '@/components'
import {queryBike} from '@/api/basic/bike'
import AddBike from "./modules/bike/AddBike"
import EditBike from "./modules/bike/EditBike"
import RemoveBike from "./modules/bike/RemoveBike"
import UpdateStateBike from "./modules/bike/UpdateStateBike";
import moment from "moment";

let that
let stateMap = {
  0: {
    text: '未使用',
    color: 'blue'
  },
  1: {
    text: '使用中',
    color: 'yellow'
  },
  2: {
    text: '维修中',
    color: 'grey'
  },
  3: {
    text: '报废',
    color: 'red'
  },
}

export default {
  name: 'BikeList',
  components: {
    STable,
    AddBike,
    EditBike,
    RemoveBike,
    UpdateStateBike,
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      bikeStates: [
        {
          value: 0,
          label: '未使用',
        },
        {
          value: 1,
          label: '使用中',
        },
        {
          value: 2,
          label: '维修中',
        },
        {
          value: 3,
          label: '报废',
        },
      ],
      columns: [
        {
          title: '车辆编号',
          dataIndex: 'bikeNumber',
          align: 'center'
        },
        {
          title: '车辆状态',
          dataIndex: 'bikeState',
          sorter: true,
          align: 'center',
          scopedSlots: {customRender: 'bikeState'},
        },
        {
          title: '目前所在站点',
          dataIndex: 'stationName',
          align: 'center',
        },
        {
          title: '添置日期',
          dataIndex: 'gmtCreate',
          align: 'center',
          customRender: (text) => moment(text).format('YYYY-MM-DD'),
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: {customRender: 'action'}
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // 统一处理查询参数，便于后端表格DTO用固定参数接收
        const queryJson = {'queryParam': JSON.stringify(this.queryParam)}
        return queryBike(Object.assign(parameter, queryJson))
            .then(res => {
              return res.result
            })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: {
          show: true, clear: () => {
            this.selectedRowKeys = []
          }
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false
    }
  },
  filters: {
    stateTextFilter (state) {
      if (stateMap[state] === undefined) {
        return ''
      }
      return stateMap[state].text
    },
    stateColorFilter (state) {
      if (stateMap[state] === undefined) {
        return ''
      }
      return stateMap[state].color
    },
  },
  beforeCreate () {
    that = this
  },
  created () {
    this.tableOption()
  },
  methods: {
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: {
            show: true, clear: () => {
              this.selectedRowKeys = []
            }
          },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    handleAuthorize (record) {
      this.$refs.authorizeBike.authorize(record.id)
    },
    handleEdit (record) {
      this.$refs.editBike.edit(record)
    },
    handleUpdateState (record) {
      this.$refs.updateStateBike.updateState(record)
    },
    gotoBikeList (record) {
      this.$router.push({name: 'bikeRecord', query: record})
    },
    handleOk () {
      this.$refs.table.clearSelected()
      this.$refs.table.refresh(false)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
  }
}
</script>
